/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.fragment;

import java.util.Calendar;

import lt.marsrutai.mobile.android.activity.intent.FastestRoutesSearchIntentHolder;
import lt.marsrutai.mobile.android.model.enums.DateTime;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;

public class TimePickerFragment extends DialogFragment {

    private OnTimeSetListener callback;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        FastestRoutesSearchIntentHolder intentHolder = new FastestRoutesSearchIntentHolder(getArguments());

        // Use the current date as the default time in the picker                
        final Calendar calendar = new DateTime(intentHolder.getCurrentlySelectedDate()).getCalendar();        
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), callback, hour, minute, DateFormat.is24HourFormat(getActivity()));
    }
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            // Register parent activity as a callback listener
            callback = (OnTimeSetListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement " + OnTimeSetListener.class);
        }
    }
}
